package com.xinghou.platform.file.chain.command.voice;

import java.io.File;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;

import com.xinghou.platform.file.model.ResourceContext;
import com.xinghou.platform.util.VoiceTools;

/**
 * 
 * @author wzk
 *
 */
public class SaveDiskCommand implements Command{

	@Override
	public boolean execute(Context context) throws Exception {
		@SuppressWarnings("unchecked")
		ResourceContext<byte[], String> resourceContext = (ResourceContext<byte[], String>)context;
		
		String absSourcePath = resourceContext.getSource().getRootPath() + resourceContext.getSource().getPath() 
				+ resourceContext.getSource().getSubPath();
		
		//如果目录不存在 则创建
		File absSourceFolder = new File(absSourcePath);
		
		if(!absSourceFolder.exists()){
			absSourceFolder.mkdirs();
		}
		
		byte[] data = (byte[])resourceContext.getRequest().getBody();
		//语音保存磁盘
		File source = new File(absSourcePath + File.separator + resourceContext.getSource().getResourceName());
		//TODO
		VoiceTools.getFileFromBytes(data, source);
		return false;
	}


}
